package com.luoxiang.socialandroid.utils

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class LoopUtil(private var lifecycleOwner: LifecycleOwner) {
    private var loopJob: Job? = null

    fun startLoopJob(interval: Long,next: () -> Unit){
        loopJob = lifecycleOwner.lifecycleScope.launch {
            while (true){
                next.invoke()
                delay(interval)
            }
        }
    }

    fun stopLoopJob(){
        loopJob?.cancel()
    }
}